package com.gxuwu.admin.controller;


import com.gxuwu.admin.config.OSSProperties;
import com.gxuwu.admin.utils.FileUtil;
import com.gxuwu.admin.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Api(tags = "文件上传下载接口")
@RestController
public class FileUploadController {

    private Logger logger= LoggerFactory.getLogger(FileUploadController.class);


    @Autowired
    private OSSProperties ossProperties;

    /**
     *
     * @param path 想要下载的文件的路径
     * @param response
     * @return
     */
    @ApiOperation(value = "文件下载")
    @PostMapping("/file/download")
    public R download(String path, HttpServletRequest request,HttpServletResponse response) throws Exception {

//        request
//                .getRequestDispatcher("http://lhq1210.oss-cn-shenzhen.aliyuncs.com/20220217/5696d6b5cd014f11ad95d9e68f15c34b.png")
//                .forward(request,response);

        return R.ok().data("filePath",path);

    }

    @ApiOperation(value = "多文件上传 swagger不支持")
    @PostMapping("/files/upload")
    public R files(MultipartFile[] files, HttpServletRequest request) throws IOException {
        R result =null;
        try {
           if (files!=null && files.length>0){
               for (MultipartFile file:files){

                   result= FileUtil.uploadFileToOss(
                           ossProperties.getEndPoint(),
                           ossProperties.getBucketName(),
                           ossProperties.getAccessKeyId(),
                           ossProperties.getAccessKeySecret(),
                           ossProperties.getBucketDomain(),
                           file.getInputStream(),
                           file.getOriginalFilename()
                   );


               }
           }
           return result;
        }catch (Exception e){
           e.printStackTrace();
           return R.error().data("msg","文件上传失败");
       }

    }

    @ApiOperation(value = "单文件上传")
    @PostMapping("/file/upload")
    public R upLoad(@RequestParam("file") MultipartFile file) throws IOException {

        if (file.isEmpty()){
            logger.debug("上传文件不能为空");
            return R.error().data("msg","上传文件不能为空");
        }

        R result = FileUtil.uploadFileToOss(
                ossProperties.getEndPoint(),
                ossProperties.getBucketName(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret(),
                ossProperties.getBucketDomain(),
                file.getInputStream(),
                file.getOriginalFilename()


        );

        return result;
    }
}
